﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace SLBehaviourSystem
{
    public abstract class CompositeNode : BehaviourNodeBase, ICompositeNode
    {
        /// <summary>临时变量，用于缓存子节点状态 </summary>
        protected BehaviourNodeState childNodeState;

        /// <summary>能够打断右侧执行的索引 </summary>
        protected List<int> InteruptIndexs = new List<int>();

        protected int FirstConditionIndex = -1;

        protected int curIndex = 0;

        public int CurIndex
        {
            get
            {
                return curIndex;
            }
        }

        protected bool isInterupted;

        protected abstract bool CheckInterruptState(BehaviourNodeState state);

        public override void AddChild(BehaviourNodeBase node)
        {
            base.AddChild(node);

            if (InteruptType == BehaviourInterruptType.InteruptByChild
                || InteruptType == BehaviourInterruptType.Both)
            {
                if (FirstConditionIndex == -1)
                {
                    IConditionNode condition = node as IConditionNode;
                    if (condition != null)
                    {
                        FirstConditionIndex = childNodes.Count - 1;
                    }
                }
            }

            if (node.InteruptType == BehaviourInterruptType.InteruptRight
                || node.InteruptType == BehaviourInterruptType.Both)
            {
                InteruptIndexs.Add(childNodes.Count - 1);
            }
        }

        protected override void OnExcute()
        {
            CheckChild();
        }

        public void CheckInterrupt(BehaviourNodeBase needCheckNode)
        {
            if (InteruptType == BehaviourInterruptType.InteruptByChild
        || InteruptType == BehaviourInterruptType.Both)
            {
                if (FirstConditionIndex != -1)
                {
                    CompositeNode composite = parent as CompositeNode;
                    if (composite != null)
                    {
                        if (curNodeState == BehaviourNodeState.Success || curNodeState == BehaviourNodeState.Failure)
                        {
                            composite.CheckInterrupt(this);
                            return;
                        }

                        if (curNodeState == BehaviourNodeState.Idle)
                        {
                            return;
                        }

                        IConditionNode condition = childNodes[FirstConditionIndex] as IConditionNode;
                        if (CheckInterruptState(condition.CheckCondition()))
                        {
                            Reset();
                            Excute();
                        }
                    }
                }
            }

            if (needCheckNode.InteruptType == BehaviourInterruptType.InteruptRight
                || needCheckNode.InteruptType == BehaviourInterruptType.Both)
            {
                for (int i = 0; i < childNodes.Count; i++)
                {
                    if (needCheckNode == childNodes[i])
                    {
                        if (curIndex > i)
                        {
                            //有触发检测默认打断
                            for (int j = i + 1; j < curIndex; j++)
                            {
                                childNodes[j].Reset();
                            }
                            childNodes[curIndex].Interupt();
                            curIndex = i;
                            needCheckNode.Reset();
                            Excute();
                        }
                    }
                }
            }




        }

        protected virtual void CheckChild()
        {

        }

        protected override void OnInterupt()
        {
            isInterupted = true;
        }

        protected override void OnReset()
        {
            curNodeState = BehaviourNodeState.Idle;
            curIndex = 0;
            isInterupted = false;
        }

        protected override void OnChildNodeComplete()
        {
            if (curNodeState == BehaviourNodeState.Success || curNodeState == BehaviourNodeState.Failure)
            {
                if (parent != null)
                {
                    parent.ChildNodeComplete();
                }
                return;
            }

            curIndex++;
            if (curIndex < childNodes.Count)
            {
                CheckChild();
            }
        }
    }
}